package com.rss.inbound.dao.impl;

// default package
// Generated Jan 10, 2013 11:00:14 PM by Hibernate Tools 3.4.0.CR1

import static org.hibernate.criterion.Example.create;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.rss.inbound.beans.RssSrc;
import com.rss.inbound.beans.Sts;
import com.rss.inbound.dao.RssSrcDAO;

/**
 * Home object for domain model class RssSrc.
 * 
 * @see .RssSrc
 * @author Hibernate Tools
 */

public class RssSrcDAOImpl implements RssSrcDAO
{

    private static final Log log	    = LogFactory
						    .getLog(RssSrcDAOImpl.class);

    private SessionFactory   sessionFactory = getSessionFactory();

    /*
     * protected SessionFactory getSessionFactory() { try { return
     * (SessionFactory) new InitialContext() .lookup("SessionFactory"); } catch
     * (Exception e) { log.error("Could not locate SessionFactory in JNDI", e);
     * throw new IllegalStateException(
     * "Could not locate SessionFactory in JNDI"); } }
     */

    public SessionFactory getSessionFactory()
    {
	return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory)
    {
	this.sessionFactory = sessionFactory;
    }

    public void persist(RssSrc transientInstance)
    {
	log.debug("persisting RssSrc instance");
	try
	{
	    sessionFactory.getCurrentSession().persist(transientInstance);
	    log.debug("persist successful");
	}
	catch (RuntimeException re)
	{
	    log.error("persist failed", re);
	    throw re;
	}
    }

    public void attachDirty(RssSrc instance)
    {
	log.debug("attaching dirty RssSrc instance");
	try
	{
	    sessionFactory.getCurrentSession().saveOrUpdate(instance);
	    log.debug("attach successful");
	}
	catch (RuntimeException re)
	{
	    log.error("attach failed", re);
	    throw re;
	}
    }

    @SuppressWarnings("deprecation")
    public void attachClean(RssSrc instance)
    {
	log.debug("attaching clean RssSrc instance");
	try
	{
	    sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
	    log.debug("attach successful");
	}
	catch (RuntimeException re)
	{
	    log.error("attach failed", re);
	    throw re;
	}
    }

    public void delete(RssSrc persistentInstance)
    {
	log.debug("deleting RssSrc instance");
	try
	{
	    sessionFactory.getCurrentSession().delete(persistentInstance);
	    log.debug("delete successful");
	}
	catch (RuntimeException re)
	{
	    log.error("delete failed", re);
	    throw re;
	}
    }

    public RssSrc merge(RssSrc detachedInstance)
    {
	log.debug("merging RssSrc instance");
	try
	{
	    RssSrc result = (RssSrc) sessionFactory.getCurrentSession().merge(
		    detachedInstance);
	    log.debug("merge successful");
	    return result;
	}
	catch (RuntimeException re)
	{
	    log.error("merge failed", re);
	    throw re;
	}
    }

    public RssSrc findById(java.lang.Integer id)
    {
	log.debug("getting RssSrc instance with id: " + id);
	try
	{
	    RssSrc instance = (RssSrc) sessionFactory.getCurrentSession().get(
		    "RssSrc", id);
	    if (instance == null)
	    {
		log.debug("get successful, no instance found");
	    }
	    else
	    {
		log.debug("get successful, instance found");
	    }
	    return instance;
	}
	catch (RuntimeException re)
	{
	    log.error("get failed", re);
	    throw re;
	}
    }

    @SuppressWarnings("unchecked")
    public List<RssSrc> findByExample(RssSrc instance)
    {
	log.debug("finding RssSrc instance by example");
	try
	{
	    List<RssSrc> results = (List<RssSrc>) sessionFactory
		    .getCurrentSession().createCriteria("RssSrc")
		    .add(create(instance)).list();
	    log.debug("find by example successful, result size: "
		    + results.size());
	    return results;
	}
	catch (RuntimeException re)
	{
	    log.error("find by example failed", re);
	    throw re;
	}
    }

    @SuppressWarnings("unchecked")
    public List<RssSrc> getAllRssSrcs()
    {
	log.debug("Finding RssSrc instance by example");
	try
	{
	    Sts sts = new Sts();
	    sts.setIdSts(1);

	    List<RssSrc> results = (List<RssSrc>) sessionFactory.getCurrentSession()
		    .createCriteria(RssSrc.class)
		    .add(Restrictions.eq("sts", sts)).list();

	    log.debug("No. of RSS Sources in the DB : " + results.size());

	    return results;
	}
	catch (RuntimeException re)
	{
	    log.error("find by example failed", re);
	    throw re;
	}
    }    
}
